package cn.edu.hunau.service.impl;

import cn.edu.hunau.dao.UserDao;
import cn.edu.hunau.dao.impl.UserDaoImpl;
import cn.edu.hunau.exception.DaoException;
import cn.edu.hunau.exception.UserNameExistsException;
import cn.edu.hunau.exception.UsernameOrPwdErrorException;
import cn.edu.hunau.pojo.User;
import cn.edu.hunau.service.UserService;
import cn.edu.hunau.utils.MD5Utils;

import java.util.Objects;

/**
 * @Author 苏佳
 * @Date 2022 12 02 11 28
 **/
public class UserServiceImpl implements UserService {
    private UserDao userDao=new UserDaoImpl();
    @Override
    public User regist(String username, String pwd) throws DaoException {
        //逻辑的实现 : 对数据格式进行判断
        String regex="^\\s*$";
        if(username.matches(regex) || pwd.matches(regex)){
            throw new RuntimeException("参数格式异常!");
        }

        //判断用户名是否重复
        checkUserName(username);


        User user = new User();
        user.setUsername(username);
        user.setPwd(pwd);


        User u = userDao.createUser(user);
        return u;
    }

    @Override
    public User loginByUsername(String username, String pwd) throws DaoException, UsernameOrPwdErrorException {
        //逻辑的实现 : 对数据格式进行判断
        String regex="^\\s*$";
        if(username.matches(regex) || pwd.matches(regex)){
            throw new RuntimeException("参数格式异常!");
        }

        User user = userDao.queryByUserNameAndPwd(username, pwd);
        if(Objects.isNull(user)){
            //用户名或者密码错误
            throw new UsernameOrPwdErrorException("用户名或者密码错误!");
        }
        return user;
    }

    @Override
    public User loginByTel(String tel, String pwd) throws DaoException {
        //逻辑的实现 : 对数据格式进行判断
        String regex="^\\s*$";
        if(tel.matches(regex) || pwd.matches(regex)){
            throw new RuntimeException("参数格式异常!");
        }

        User user = userDao.queryByTelAndPwd(tel, pwd);
        if(Objects.isNull(user)){
            //手机号码或者密码错误
            throw new UsernameOrPwdErrorException("用户名或者密码错误!");
        }
        return user;
    }

    @Override
    public boolean checkUserName(String username) throws UserNameExistsException {
        User user = userDao.queryByUsername(username);
        if(!Objects.isNull(user)){
            //用户名重复
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean updatePwdByUid(String oldPwd,String newPwd, Integer uid) throws DaoException {
        //逻辑判断两次密码是否为空
        String regex="^\\s*$";
        if(newPwd.matches(regex) || oldPwd.matches(regex)){
            throw new RuntimeException("参数格式异常!");
        }

        //检验旧密码是否正确
        String pwd = userDao.queryPwdByUid(uid);
        if(pwd.equals(MD5Utils.getPWD(oldPwd))){
            //判断前后两次密码是否相同
            if(oldPwd!=newPwd){
                userDao.updatePwd(newPwd,uid);
                return true;
            }else{
                //前后两次密码相同 不予修改
                return false;
            }
        }else{
            throw new RuntimeException("原密码错误!");
        }


    }

    @Override
    public boolean bandingTel(String tel, Integer uid) throws DaoException {
        //逻辑的实现 : 对数据格式进行判断
        String regex="^\\s*$";
        if(tel.matches(regex)||tel.length()!=11){
            throw new RuntimeException("参数格式异常!");
        }
        //判断是否是11位的数字
        if (User.isNumeric(tel)) {
            userDao.bingingTel(tel, uid);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public User findMyInforByUid(Integer uid) throws DaoException {
        User user = userDao.queryInforByUid(uid);
        return user;
    }
}
